<template>
  <a-modal
    :width="1200"
    :title="title"
    :visible="innerVisible"
    @cancel="handleCancel"
    @ok="handleOk"
    cancelText="关闭"
  >
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-item label="合同单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractCompany']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="供货企业" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['supplier']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractNumber']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractName']" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['contractType']" :trigger-change="true" dictCode="contract_type"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['contractPrice']"  style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="合同状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['contractState']" :trigger-change="true" dictCode="contract_state" />
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="合同完成比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['completionRatio']" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="签订日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择签订日期" v-decorator="['signingDate']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="有限期限" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['limitedTerm']" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="已付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['amountPaid']"  style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="已开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['invoicedAmount']"  style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="未付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['unpaidAmount']" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="未开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['amountNotInvoiced']"  style="width: 100%"/>
            </a-form-item>
          </a-col>

          <div class="ant-col ant-col-3 ant-form-item-label"><label  title="意见" style="text-align: right">意见</label></div>
          <a-col :span="20" >
            <a-form-item  :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea style="width: 100%; height: 100px;min-width: 880px;" v-decorator="['opinion']"  placeholder="请输入意见"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="按时供货计划明细" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="deliveryDetailTable.loading"
          :columns="deliveryDetailTable.columns"
          :dataSource="deliveryDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="按计划完成工作明细" :key="refKeys[1]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="workDetailsTable.loading"
          :columns="workDetailsTable.columns"
          :dataSource="workDetailsTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="按计划完成支付明细" :key="refKeys[2]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[2]"
          :loading="paymentDetailsTable.loading"
          :columns="paymentDetailsTable.columns"
          :dataSource="paymentDetailsTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
  </a-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction,auditOpinion } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"


  export default {
    name: 'ContractInformationAudit',
    mixins: [JEditableTableMixin],
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,

    },
    data() {
      return {
        innerVisible : false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['deliveryDetail', 'workDetails', 'paymentDetails', ],
        tableKeys:['deliveryDetail', 'workDetails', 'paymentDetails', ],
        activeKey: 'deliveryDetail',
        // 按时供货计划明细
        deliveryDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '涉密类型',
              key: 'classifiedTypes',
              type: FormTypes.select,
              dictCode:"classified_types",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '设备类型',
              key: 'deviceType',
              type: FormTypes.select,
              dictCode:"device_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '生产厂商',
              key: 'manufacturers',
              type: FormTypes.select,
              dictCode:"manufacturers",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: 'cpu芯片',
              key: 'cpu',
              type: FormTypes.select,
              dictCode:"cpu",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '操作系统',
              key: 'operatingSystem',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '具体型号',
              key: 'specificModels',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '计划完成数量',
              key: 'plannedNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '计划完成时间',
              key: 'plannedTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '目前完成数量',
              key: 'completeNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '未完成数量',
              key: 'unfinishedNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '计量单位',
              key: 'measuringUnit',
              type: FormTypes.select,
              dictCode:"measuring_unit",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 按计划完成工作明细
        workDetailsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '事项名称',
              key: 'itemName',
              type: FormTypes.select,
              dictCode:"item_name",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '计划完成时间',
              key: 'plannedTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '实际完成时间',
              key: 'actualTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '完成状态',
              key: 'workStatus',
              type: FormTypes.select,
              dictCode:"work_status",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 按计划完成支付明细
        paymentDetailsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '支付类型',
              key: 'paymentType',
              type: FormTypes.select,
              dictCode:"payment_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '计划完成时间',
              key: 'plannedTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '实际完成时间',
              key: 'actualTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '支付金额',
              key: 'paymentAmount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '完成状态',
              key: 'paymentStatus',
              type: FormTypes.select,
              dictCode:"payment_status",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/business/contractInformation/add",
          edit: "/business/contractInformation/edit",
          queryById: "/business/contractInformation/queryById",
          examineUrl:"/business/contractInformation/audit",
          deliveryDetail: {
            list: '/business/contractInformation/queryDeliveryDetailByMainId'
          },
          workDetails: {
            list: '/business/contractInformation/queryWorkDetailsByMainId'
          },
          paymentDetails: {
            list: '/business/contractInformation/queryPaymentDetailsByMainId'
          },

        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      addBefore(){
        this.form.resetFields()
        this.deliveryDetailTable.dataSource=[]
        this.workDetailsTable.dataSource=[]
        this.paymentDetailsTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'opinion','contractCompany','supplier','contractNumber','contractName','contractType','contractPrice','contractState','completionRatio','signingDate','limitedTerm','amountPaid','invoicedAmount','unpaidAmount','amountNotInvoiced')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.deliveryDetail.list, params, this.deliveryDetailTable)
          this.requestSubTableData(this.url.workDetails.list, params, this.workDetailsTable)
          this.requestSubTableData(this.url.paymentDetails.list, params, this.paymentDetailsTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          deliveryDetailList: allValues.tablesValue[0].values,
          workDetailsList: allValues.tablesValue[1].values,
          paymentDetailsList: allValues.tablesValue[2].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'opinion','contractCompany','supplier','contractNumber','contractName','contractType','contractPrice','contractState','completionRatio','signingDate','limitedTerm','amountPaid','invoicedAmount','unpaidAmount','amountNotInvoiced'))
     },
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      handleCancel() {
        this.innerVisible = false
      },
      handleOk () {
        let opinion = this.form.getFieldValue('opinion');
        let id = this.model.id;
        const that = this;
        auditOpinion(this.url.examineUrl, opinion, id).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.$emit('ok');
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
          that.close();
        })
      },
      auditInfo(record){
        //this.queryParam.Id = id;
        //this.loadData();
        //this.Id = id;
        this.innerVisible = true;

          if (typeof this.editBefore === 'function') this.editBefore(record)
          this.innerVisible = true
          this.visible = false
          this.activeKey = this.refKeys[0]
          this.form.resetFields()
          this.model = Object.assign({}, record)
          if (typeof this.editAfter === 'function') this.editAfter(this.model)

      },

    }
  }
</script>

<style scoped>
</style>